import { Component, Host, ChangeDetectionStrategy, ChangeDetectorRef, OnInit } from '@angular/core';
import { AppComponent } from '../../../../../app.component';
import { CompanyService } from '../../company.service';
import { CompanyModel } from '../../company.model';
import { CompanyGlobalCompactService, GCAssesment } from '../companyGlobalCompact.service';
import { CompanyGCSummaryModel } from '../models/companyGCsummary.model';
import { CompanyGCPrincipleModel } from '../models/companyGCprinciple.model';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';

@Component({
    moduleId: module.id,
    templateUrl: 'global-compact-pillar-detail.component.html',
    styleUrls: [ '../companyGlobalCompact.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class GlobalCompactPillarDetailComponent implements OnInit {
    public company: CompanyModel;
    public gcSummary: CompanyGCSummaryModel;
    public gcPrinciples: CompanyGCPrincipleModel[];
    public gCAssessment: typeof GCAssesment = GCAssesment;
    public pillars = [];
    public pillar;
    public pillarData;
    public hasBothLists: boolean;
    public pillarPrinciples;
    private sub: Subscription;


    constructor(
        private route: ActivatedRoute,
        private cdRef: ChangeDetectorRef,
        public companyService: CompanyService,
        public companyGlobalCompactService: CompanyGlobalCompactService,
        @Host() private rootComponent: AppComponent
    ) {
        this.pillars = companyGlobalCompactService.getPillars();
    }

    ngOnInit() {
        this.sub = this.route.params.subscribe(params => {
            window.scrollTo(0, 0);
            this.pillar = this.pillars.find(pillar => (pillar.id).toString() === params['id']);
            this.cdRef.markForCheck();

            this.companyService.company.subscribe(company => {
                this.company = company;
                this.companyService.setHeaderData(undefined);

                this.companyGlobalCompactService.getByCompany(this.company.id).subscribe(summary => {
                    this.gcSummary = summary;
                    this.cdRef.markForCheck();
                    this.rootComponent.finishLoading();
                    this.companyGlobalCompactService.principleData.subscribe(results => {
                        // set permissions to each list
                        const nonCompliantData = [].concat(results[0] || []);
                        const watchlistData = [].concat(results[1] || []);
                        this.hasBothLists = results[0] !== false && results[1] !== false;

                        // combine lists
                        this.gcPrinciples = [].concat(nonCompliantData, watchlistData);
                        this.pillarData = this.getPillarData(this.pillar.id);

                        // setup principles
                        this.pillarPrinciples = {};
                        this.pillarData.forEach((pillarData) => {
                            const id = pillarData.principle.id;
                            this.pillarPrinciples[id] = this.getPillarPrinciple(pillarData.principle.id);
                        });
                        this.cdRef.markForCheck();
                    });

                });
            });
        });
    }

    public getPrincipleData(principleId: string) {
        const id = this.pillarPrinciples[principleId].id;

        if (!this.pillarPrinciples[principleId].impact) {
            this.companyGlobalCompactService.getPrincipleIndicatorImpact(id).subscribe(data => {
                this.pillarPrinciples[principleId].impact = data[0];
                this.cdRef.markForCheck();
            }, error => { this.pillarPrinciples[principleId].impact = undefined; });
        }

        if (!this.pillarPrinciples[principleId].conclusion) {
            this.companyGlobalCompactService.getPrincipleConclusion(id).subscribe(data => {
                this.pillarPrinciples[principleId].conclusion = data;
                this.cdRef.markForCheck();
            }, error => { this.pillarPrinciples[principleId].conclusion = undefined; });
        }

        if (!this.pillarPrinciples[principleId].management) {
            this.companyGlobalCompactService.getPrincipleIndicatorManagement(id).subscribe(data => {
                this.pillarPrinciples[principleId].management = data[0];
                this.cdRef.markForCheck();
            }, error => { this.pillarPrinciples[principleId].management = undefined; });
        }
    }

    public getPillarData(pillarId: number): CompanyGCPrincipleModel[] {
        return this.gcPrinciples.filter(x => x.principle.pillar.id === pillarId);
    }

    public getPillarPrinciple(principleId: number): CompanyGCPrincipleModel {
        return this.pillarData
            ? this.pillarData.find(x => x.principle.id === principleId)
            : undefined;
    }
}